package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.QueryUserCardRequest;
import sz.szsmk.citizencard.model.network.bean.request.UnbindCardReq;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.IbaseBean;
import sz.szsmk.citizencard.model.network.bean.response.ModifyNickNameReq;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserCardResponse;
import sz.szsmk.citizencard.mvp.contract.IHomeContact;
import sz.szsmk.citizencard.mvp.contract.IRoutineCardContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/16 15:43
 * description:
 */
public class RoutineCardPresenter extends IBasePresenter
        implements IRoutineCardContact.IRoutineCardPresenter<IRoutineCardContact.IRoutineCardView> {
    private NetworkDateSources mNetworkDateSource;

    @Inject
    public RoutineCardPresenter(GsdxNetApi gsdxNetApi) {

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }


    @Override
    public void queryUserCard(String type, String origin) {
        QueryUserCardRequest cardRequest = new QueryUserCardRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        cardRequest.setAccess_token(access_token);
        cardRequest.setType("00");
        cardRequest.setOrigin(origin);
        String json = new Gson().toJson(cardRequest);
//        L.e(Constant.TAG, json);
        mNetworkDateSource.queryUserCard(cardRequest, new SubscribeResult<QueryUserCardResponse>() {
            @Override
            protected void onOk(QueryUserCardResponse response) {
                String json = new Gson().toJson(response);
                L.e(Constant.TAG, "queryUserCard----->" + json);
                ((IRoutineCardContact.IRoutineCardView) mView).queryUserCardSuccess(response);
            }

            @Override
            protected void onFailure(QueryUserCardResponse response) {
                super.onFailure(response);
                ((IRoutineCardContact.IRoutineCardView) mView).queryUserCardFalie(response.getMessage());
            }

            @Override
            protected void onNext(QueryUserCardResponse response) {
                super.onNext(response);
                ((IRoutineCardContact.IRoutineCardView) mView).queryUserCardFalie(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((IHomeContact.IHomeView) mView).queryUserCardFail(throwable.getMessage());
            }
        });
    }

    @Override
    public void unBindCard(String accessToken, String cardNo) {
        UnbindCardReq unbindCardReq = new UnbindCardReq();
        unbindCardReq.setAccess_token(accessToken);
        unbindCardReq.setCardNo(cardNo);
        String json = new Gson().toJson(unbindCardReq);
        L.e(Constant.TAG, "unBindCard---->" + json);
        mNetworkDateSource.unBindCard(unbindCardReq, new SubscribeResult<IbaseBean>() {
            @Override
            protected void onOk(IbaseBean response) {
                String toJson = new Gson().toJson(response);
                L.e(Constant.TAG, "unBindCard---->" + toJson);
                ((IRoutineCardContact.IRoutineCardView) mView).unBindCardSucc();
            }

            @Override
            protected void onFailure(IbaseBean response) {
                super.onFailure(response);
                ((IRoutineCardContact.IRoutineCardView) mView).unBindCardFail(response.getMessage());

            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((IRoutineCardContact.IRoutineCardView) mView).unBindCardFail(throwable.getMessage());

            }
        });
    }

    @Override
    public void modifyCardNickName(String cardNo) {
        ModifyNickNameReq modifyNickNameReq = new ModifyNickNameReq();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        modifyNickNameReq.setAccess_token(access_token);
        modifyNickNameReq.setCard_nick_name("测试昵称");
        modifyNickNameReq.setCard_no(cardNo);
        mNetworkDateSource.modifyCardNickName(modifyNickNameReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                ((IRoutineCardContact.IRoutineCardView) mView).modifyCardNickNameSucc();
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                ((IRoutineCardContact.IRoutineCardView) mView).modifyCardNickNameFail();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void queryUserCardInfoTemp() {

    }
}